﻿$ErrorState = 2
$ErrorMessage = ""
$Trace = ""
$Value = ""
$Error.Clear()

$virstoNamespace = "root\virtualization\storage"
$snapName = '%SNAPNAME%'
$snapId = ""

try {

  $Trace += 'Connecting to Virsto...  \n'

  $snapshot = gwmi -query "select * from Virsto_VSSnapshot where Name='$snapName'" -namespace $virstoNamespace
  if ($snapshot -eq $null) {
    
    $Trace += 'Virsto SnapShot not exist...  \n'
    $ErrorState = 2
  }
  else
  {
	 $ErrorState = 0
	 $Trace += 'Virsto SnapShot exists...  \n'
     $snapId = $snapshot.Id
     $Trace += 'Virsto SnapShot Id is: ' + $snapId + ' \n'
  }

}
catch
{
  $ErrorState = 2
  $ErrorMessage = $error[0].Exception.ToString()
}
Finally
{
  $Trace += 'ErrorState:  ' + $ErrorState + ' \n'
  $Trace += 'ErrorMessage: ' + $ErrorMessage + ' \n'
  $Trace += 'SnapId: ' + $snapId + ' \n'
}

$Results = @($ErrorState, $ErrorMessage, $Trace, $snapId) 
Return $Results


